package gameobject.item;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class Item implements Savable, Cloneable {
    protected int quantity = 1;

    public Item() {}

    public Item(int quantity) {
	this.quantity = quantity;
    }

    public int getQuantity() {
	return quantity;
    }

    public void setQuantity(int quantity) {
	this.quantity = quantity;
    }

    public void removeOne() {
	quantity--;
    }

    public void changeQuantity(int amount) {
	quantity += amount;
    }
    
    @Override
    public void write(JmeExporter ex) throws IOException {
	OutputCapsule capsule = ex.getCapsule(this);
	capsule.write(quantity, "quantity", 1);
    }

    @Override
    public void read(JmeImporter im) throws IOException {
	InputCapsule capsule = im.getCapsule(this);
	quantity = capsule.readInt("quantity", 1);
    }

    @Override
    public boolean equals(Object obj) {
	if (obj == null) {
	    return false;
	}
	if (getClass() != obj.getClass()) {
	    return false;
	}
	return true;
    }

    @Override
    public int hashCode() {
	int hash = 3;
	return hash;
    }

    @Override
    public String toString() {
	return this.getClass().getSimpleName()+"("+quantity+")";
    }
    
    @Override
    public Item clone() {
        try {
            Item clone = getClass().cast(super.clone());
            clone.setQuantity(quantity);
            return clone;
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(Item.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
